import { Component } from '@angular/core';
import { IonicPage, NavController, ViewController, ModalController, NavParams } from 'ionic-angular';
import { Storage } from '@ionic/storage';
import { File } from '@ionic-native/file';

import { SettingsModePage } from '../settings-mode/settings-mode';
import { SettingsGeneralPage } from '../settings-general/settings-general';
import { SettingsLanguagePage } from '../settings-language/settings-language';
import { SettingsPrintPage } from '../settings-print/settings-print';
import { OfflineService } from "../../providers/services.offline";
import { PrinterService } from "../../providers/services.printer";
import { GlobalData } from '../../providers/GlobalData';
import { LoginPage } from '../login/login';

/**
 * Generated class for the SettingsPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-settings',
  templateUrl: 'settings.html',
})
export class SettingsPage {

  constructor(public navCtrl: NavController,
    public modalCtrl: ModalController,
    public navParams: NavParams,
    public viewCtrl: ViewController,
    public offline: OfflineService,
    public printer: PrinterService,
    public storage: Storage,
    private global: GlobalData,
    private file: File) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SettingsPage');

  }

  modeSettings() {
    let modal = this.modalCtrl.create(SettingsModePage);
    modal.onDidDismiss(data => {
      console.log(data);
      if (data != null && data == 'tologin') {
        this.dismiss(data);
      }
    });
    modal.present();
  }

  generalSettings() {
    let modal = this.modalCtrl.create(SettingsGeneralPage);
    modal.present();
  }

  languageSettings() {
    let modal = this.modalCtrl.create(SettingsLanguagePage);
    modal.present();
  }

  printerSettings() {
    let modal = this.modalCtrl.create(SettingsPrintPage);
    modal.present();
  }

  // dismiss table list modal
  dismiss(type) {
    this.viewCtrl.dismiss(type);
  }

  // test
  test() {

    /*this.offline.getInvoiceTemplateCss('80mm').then((content) => {
      alert(content);
      alert(JSON.stringify(content));
    }, (err) => {
      alert(err.toString());
      alert(JSON.stringify(err));
    });*/

    /*if (!this.global.getScope('epsonPrinterReady')) {
      this.printer.epsonInit('192.168.1.237', '8008', 'local_printer');
    }
    else {
      //this.printer.esponOpenDrawer();
        this.printer.epsonPrepareMultipleCanvas('h1 { font-size:40px; }', ['<h1>hahahaha</h1>', '<h1>hohohoho</h1>', '<h1>hehehehe</h1>']);
        this.printer.epsonPrintCanvas();
    }*/

    this.storage.get('settings').then((settings) => {

      //this.offline.startServer(8181);
      this.offline.scanServerQrCode();

      /*this.offline.getOfflinePhoto({
        token: settings.token,
        locale: settings.locale
      }).then(() => {
        alert('done');
      }, (err) => {
        alert(err);
      });*/

      /*this.offline.getOfflineData({
        token: settings.token,
        locale: settings.locale,
        category: 'POS',
        type: 'SQLite'
      }).then(() => {
        console.log('getOfflineData');
      }, (err) => {
        alert(err);
      });*/

    });
  }
  test2() {
    this.offline.startServer(8181);
  }
  testInvoicePdf() {
    this.file.readAsDataURL(this.file.applicationDirectory + 'www/assets/img/', 'posify_r.png').then((logo) => {
      this.offline.getInvoiceHtml({
        size: '80mm',
        pages: [
          {
            COMPANY_LOGO: logo,  // company logo in base64 data
            SHOW_COMPANY_LOGO: '',  // '', 'none'. show/hide company logo
            SHOP_TEL: '28119139',  // shop tel no
            SHOP_ADDRESS: '旺角 旺角道 26號',  // shop address
            SHOW_SHOP_INFO: '',  // '', 'none'. show/hide shop info
            INVOICE_TITLE: '收據',  // '收據', '發票'
            SHOW_INVOICE_TITLE: '',  // '', 'none'. show/hide invoice title
            TICKET_TYPE: '檯號',  // '檯號', '堂食'
            TICKET_NO: '1A',  // ticket no. or table no.
            INVOICE_TYPE: '堂食',  // '堂食', '外賣'
            MEMBER_CLASS: '訪客',  // member class
            SHOW_MEMBER_CLASS: '',  // '', 'none'. show/hide member class
            INVOICE_DATE: '2017-06-17 13:38:05',  // invoice date
            INVOICE_NO: '0006',  // invoice no.
            SHOW_INVOICE_NO: '',  // '', 'none'. show/hide invoice no
            SALESMAN: 'arcadmin@phi.me',  // salesman
            SHOW_SALESMAN: '',  // '', 'none'. show/hide salesman
            DEPARTMENT_NAME: '廚房',  // department name
            SHOW_DEPARTMENT_NAME: 'none',  // '', 'none'. show/hide department name
            SHOW_PRICE: '',  // '', 'none'. show/hide price
            PRODUCT_ITEM: [  // products
              {
                PRODUCT_NAME: '士多啤梨奶<br />走甜',
                QTY: '1',
                DISCOUNT: '-',
                SUB_TOTAL: '$36.0',
                SHOW_PRICE: '',  // 'block', 'none'. show/hide price
              },
              {
                PRODUCT_NAME: '士多啤梨奶<br />走甜',
                QTY: '1',
                DISCOUNT: '-',
                SUB_TOTAL: '$36.0',
                SHOW_PRICE: '',  // 'block', 'none'. show/hide price
              },
              {
                PRODUCT_NAME: '士多啤梨奶<br />走甜',
                QTY: '1',
                DISCOUNT: '-',
                SUB_TOTAL: '$36.0',
                SHOW_PRICE: '',  // 'block', 'none'. show/hide price
              },
            ],
            CHARGE_ITEM: [
              {
                CHARGE_NAME: '青春損失費',
                CHARGE_VALUE: '10%',
                CHARGE_AMOUNT: '($100.0)',
              },
              {
                CHARGE_NAME: '青春損失費',
                CHARGE_VALUE: '10%',
                CHARGE_AMOUNT: '($100.0)',
              },
            ],
            ITEM_TOTAL: '$130.0',
            GRAND_TOTAL: '$130.0',
            CASH_RECEIVED: '$150.0',
            CHANGES: '$20.0',
            SHOW_PAYMENT_DETAIL: '',
            SHOW_RECEIVE_DETAIL: '',
            DEVICE_NO: 'B028172',
            OCTOPUS_NO: '9987766',
            AMOUNT_DEDUCTED: '$-130.0',
            REMAINING_VALUE: '$999.0',
            DEDUCT_DATE: '2017-06-17 13:38:05',
            SHOW_OCTOPUS: '',
            SHOW_FOOTER: '',
          },
          /*{
            COMPANY_LOGO: logo,  // company logo in base64 data
            SHOW_COMPANY_LOGO: 'none',  // '', 'none'. show/hide company logo
            SHOP_TEL: '28119139',  // shop tel no
            SHOP_ADDRESS: '旺角 旺角道 26號',  // shop address
            SHOW_SHOP_INFO: 'none',  // '', 'none'. show/hide shop info
            INVOICE_TITLE: '收據',  // '收據', '發票'
            SHOW_INVOICE_TITLE: 'none',  // '', 'none'. show/hide invoice title
            TICKET_TYPE: '檯號',  // '檯號', '堂食'
            TICKET_NO: '1A',  // ticket no. or table no.
            INVOICE_TYPE: '堂食',  // '堂食', '外賣'
            MEMBER_CLASS: '訪客',  // member class
            SHOW_MEMBER_CLASS: 'none',  // '', 'none'. show/hide member class
            INVOICE_DATE: '2017-06-17 13:38:05',  // invoice date
            INVOICE_NO: '0006',  // invoice no.
            SHOW_INVOICE_NO: '',  // '', 'none'. show/hide invoice no
            SALESMAN: 'arcadmin@phi.me',  // salesman
            SHOW_SALESMAN: 'none',  // '', 'none'. show/hide salesman
            DEPARTMENT_NAME: '廚房',  // department name
            SHOW_DEPARTMENT_NAME: '',  // '', 'none'. show/hide department name
            SHOW_PRICE: 'none',  // '', 'none'. show/hide price
            PRODUCT_ITEM: [  // products
              {
                PRODUCT_NAME: '士多啤梨奶<br />走甜',
                QTY: '1',
                DISCOUNT: '-',
                SUB_TOTAL: '$36.0',
                SHOW_PRICE: 'none',  // 'block', 'none'. show/hide price
              },
              {
                PRODUCT_NAME: '士多啤梨奶<br />走甜',
                QTY: '1',
                DISCOUNT: '-',
                SUB_TOTAL: '$36.0',
                SHOW_PRICE: 'none',  // 'block', 'none'. show/hide price
              },
              {
                PRODUCT_NAME: '士多啤梨奶<br />走甜',
                QTY: '1',
                DISCOUNT: '-',
                SUB_TOTAL: '$36.0',
                SHOW_PRICE: 'none',  // 'block', 'none'. show/hide price
              },
            ],
            CHARGE_ITEM: [],
            ITEM_TOTAL: '$130.0',
            GRAND_TOTAL: '$130.0',
            CASH_RECEIVED: '$150.0',
            CHANGES: '$20.0',
            SHOW_PAYMENT_DETAIL: 'none',
            SHOW_RECEIVE_DETAIL: 'none',
            DEVICE_NO: 'B028172',
            OCTOPUS_NO: '9987766',
            AMOUNT_DEDUCTED: '$-130.0',
            REMAINING_VALUE: '$999.0',
            DEDUCT_DATE: '2017-06-17 13:38:05',
            SHOW_OCTOPUS: 'none',
            SHOW_FOOTER: 'none',
          },*/
        ]
      }).then((res) => {
        this.printer.epsonPrepareMultipleCanvas(res.css, res.htmls);
      }, (err) => {
        alert(err);
      });
    });
  }
  // testConnectPrinter() {
  //   this.printer.epsonInit('192.168.1.239', '8008', 'local_printer');
  // }
  // testPrint() {
  //   this.printer.epsonPrintCanvas();
  // }

}
